package com.DeliveryProgram.www.servlet;

import com.DeliveryProgram.www.constant.BuScreen;
import com.DeliveryProgram.www.constant.Screen;
import com.DeliveryProgram.www.constant.UScreen;
import com.DeliveryProgram.www.dao.BusLoad;
import com.DeliveryProgram.www.dao.UserLoad;
import com.DeliveryProgram.www.dao.UserSave;
import com.DeliveryProgram.www.po.Address;
import com.DeliveryProgram.www.po.Business;
import com.DeliveryProgram.www.po.Menu;
import com.DeliveryProgram.www.po.User;
import com.DeliveryProgram.www.service.Recation;
import com.DeliveryProgram.www.util.Scanners;
import com.DeliveryProgram.www.util.Screens;

import java.util.ArrayList;
import java.util.List;

/**用户操作系统*/
public class Use {

    private static List<Menu>list=new ArrayList<Menu>();
    /** 用户操作主系统*/
    public static void doUser(User us) {
        while (true) {
            Screen.main();
            switch (Scanners.getChoice()) {
                /**退出*/
                case 0: {
                    System.out.println("感谢您的使用");
                    Scanners.pause();
                    return;
                }
                /**查看自己信息*/
                case 1: {
                    checkUser(us);
                    break;
                }
                /**点外卖*/
                case 2: {
                    buy(us);
                    break;
                }
                default: {
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /** 用户查找个人信息*/
    public static void checkUser(User us) {
        while (true) {
            Screens.clear();
            UScreen.checkUser(us);
            switch (Scanners.getChoice()) {
                /**离开界面*/
                case 0: {
                    return;
                }
                /**添加余额*/
                case 1: {
                    System.out.println("请输入添加金额");
                    us.addMoney(Scanners.getChoice());
                    UserSave.changeMoney(us);
                    break;
                }
                /**管理地址*/
                case 2: {
                    checkAddress(us);
                    break;
                }
                /**查看折扣*/
                case 3: {
                    checkCoupons(us);
                    break;
                }
                /**查看订单*/
                case 4: {
                    checkOrders(us);
                    break;
                }
                /**查看评论*/
                case 5: {
                    checkComment(us);
                    break;
                }
                default: {
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /** 用户管理地址主界面*/
    public static void checkAddress(User us) {
        Screens.clear();
        while (true) {
            UScreen.checkAddress(us);
            System.out.println("0-退出 | 1-添加新的地址 | 2-修改已有地址");
            switch (Scanners.getChoice()) {
                case 0: {
                    return;
                }
                case 1: {
                    System.out.println("请输入新的地址");
                    String addr = Scanners.getScanner();
                    System.out.println("请输入电话");
                    String ph = Scanners.getScanner();
                    us.getAddresses().add(new Address(addr, ph));
                    UserSave.addAddress(us);
                    UserLoad.getAddrNewId(us);
                    break;
                }
                case 2: {
                    Screens.clear();
                    changeAddr(us);
                    break;
                }
                default: {
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /** 用户修改已有地址 */
    public static void changeAddr(User us) {
        while (true) {
            int choice = 0;
            /**防止用户输入不存在的订单*/
            do {
                UScreen.checkAddress(us);
                System.out.println("请输入需要修改的地址编号 | 0-返回");
                choice = Scanners.getChoice();
                if (choice - 2 > us.getAddresses().size()) {
                    System.out.println("请输入存在的订单");
                    Scanners.pause();
                    Screens.clear();
                }
            } while (choice - 2 > us.getAddresses().size());
            switch (choice) {
                /**输入0离开订单查询*/
                case 0:
                    return;
                default: {
                    /**获取本来订单编号*/
                    choice -= 1;
                    checkAddr(us, choice);
                    break;
                }

            }
        }
    }

    /** 对某一个地址进行修改*/
    public static void checkAddr(User us, int num) {
        while (true) {
            Screens.clear();
            System.out.println("地址: " + us.getAddresses().get(num).getAddr() + " | 电话号码:" + us.getAddresses().get(num).getPhone());
            System.out.println("--------------------------------------------------------------------------------------------");
            System.out.println(" 0-返回 | 1-修改地址 | 2-修改电话 | 3-删除地址");
            switch (Scanners.getChoice()) {
                case 0: {
                    return;
                }
                case 1: {
                    System.out.println("请输入新的地址");
                    us.getAddresses().get(num).setAddr(Scanners.getScanner());
                    UserSave.updateAddr(us, num);
                    break;
                }
                case 2: {
                    System.out.println("请输入新的电话");
                    us.getAddresses().get(num).setPhone(Scanners.getScanner());
                    UserSave.updateAddr(us, num);
                    break;
                }
                case 3: {
                    UserSave.deleteAddr(us.getAddresses().remove(num));
                    Screens.clear();
                    return;
                }
                default: {
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**查看着折扣*/
    public static void checkCoupons(User us) {
        Screens.clear();
        while (true) {
            UScreen.checkCoupons(us);
            if (Scanners.getChoice() == 0) {
                return;
            }
        }
    }

    /**
     * 查看订单
     */
    public static void checkOrders(User us) {
        Screens.clear();
        while (true) {
            int choice = 0;
            /**防止用户输入不存在的订单*/
            do {
                UScreen.checkOrders(us);
                choice = Scanners.getChoice();
                if (choice - 2 > us.getOrders().size()) {
                    System.out.println("请输入存在的订单");
                    Scanners.pause();
                    Screens.clear();
                }
            } while (choice - 2 > us.getOrders().size());
            switch (choice) {
                /**输入0离开订单查询*/
                case 0:
                    return;
                default: {
                    /**获取本来订单编号*/
                    choice -= 1;
                    checkOder(us, choice);
                    break;
                }
            }

        }
    }

    /**
     * 查看具体订单
     */
    public static void checkOder(User us, int num) {
        Screens.clear();
        while (true) {
            UScreen.detailOder(us, num);
            if (Scanners.getChoice() == 0) {
                return;
            }
        }
    }

    /**
     * 查看评论历史
     */
    public static void checkComment(User us) {
        Screens.clear();
        while (true) {
            UScreen.checkComment(us);
            if (Scanners.getChoice() == 0) {
                return;
            }
        }
    }

    /** 外卖主系统*/
    public static void buy(User us) {
        List<Business> list = new ArrayList<Business>();
        /**获取商家基本信息*/
        BusLoad.getAllBusiness(list);
        while (true) {
            Screens.clear();
            int choice = 0;
            /**防止用户输入不存在的订单*/
            do {
                UScreen.showAllBusiness(list);
                choice = Scanners.getChoice();
                if (choice - 2 > list.size()) {
                    System.out.println("请输入存在的订单");
                    Scanners.pause();
                    Screens.clear();
                }
            } while (choice - 2 > list.size());
            switch (choice) {
                /**输入0离开订单查询*/
                case 0:
                    return;
                default: {
                    /**获取本来订单编号*/
                    choice -= 1;
                    inBusiness(us,list.get(choice));
                    Screens.clear();
                    break;
                }
            }
        }

    }

    /**进入某一个商家开始操作*/
    public static void inBusiness(User us,Business bu){
        Business.getMessage(bu);
        while(true){
            Screens.clear();
            UScreen.showBusiness(bu);
            System.out.println("0-退出");
            System.out.println("1-开始点单");
            System.out.println("2-查看评论");
            if(bu.getCoupons().size()!=0) {
                System.out.println("3-领取折扣");
            }
            switch (Scanners.getChoice()){
                case 0:{
                    return;
                }
                case 1:{
                    buying(us,bu);
                    break;
                }
                case 2:{
                    BuScreen.showComment(bu);
                    Scanners.pause();
                    break;
                }
                case 3:{
                    if(bu.getCoupons().size()!=0) {
                        Recation.getCoupons(us,bu);
                        break;
                    }
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

    /**用户开始点单*/
    public static void buying(User us,Business bu){
        while(true){
            Screens.clear();

            int choice = 0;
            /**防止用户输入不存在的订单*/
            do {
                UScreen.showBusiness(bu);
                System.out.println("输入编号加入订单 | 0-退出");
                choice = Scanners.getChoice();
                if (choice - 2 > bu.getMenu().size()) {
                    System.out.println("请输入存在的订单");
                    Scanners.pause();
                    Screens.clear();
                }
            } while (choice - 2 > bu.getMenu().size());
            switch (choice) {
                /**输入0离开订单查询*/
                case 0:
                    return;
                default: {
                    /**获取本来订单编号*/
                    choice -= 1;
                    putIn(list,bu,choice);
                    isEnd(list,bu,us);
                }
            }
        }
    }

    /**将菜放进订单里*/
    public static void putIn(List<Menu> list,Business bu,int num){

        for(int i=0;i<list.size();i++){
            if(bu.getMenu().get(num).getId()==list.get(i).getId()){
                list.get(i).addMount();
                return;
            }
        }
        list.add(bu.getMenu().get(num));
        list.get(list.size()-1).setMount(1);
    }

    /**用户选择是否结算*/
    public static void isEnd(List<Menu> list,Business bu,User us){
        while(true){
            System.out.println("是否结算");
            System.out.println("1-是 | 2-否");
            switch (Scanners.getChoice()){
                case 1:{
                    Recation.makeOrder(list,bu,us);
                    return;
                }
                case 2:{
                    return;
                }
                default:{
                    System.out.println("请输入正确的选项");
                    /**让用户确认*/
                    Scanners.pause();
                    Screens.clear();
                    break;
                }
            }
        }
    }

}
